package com.qyhl.webtv.commonlib.item;

import android.content.Context;
import androidx.cardview.widget.CardView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.makeramen.roundedimageview.RoundedImageView;
import com.orhanobut.hawk.Hawk;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.commonlib.R;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.news.GlobalNewsBean;
import com.zhy.adapter.recyclerview.base.ItemViewDelegate;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.Map;

/**
 * Created by qyhl on 2018/4/20.
 * 圈子图
 */

public class ItemCirclePicture implements ItemViewDelegate<GlobalNewsBean> {
    private Context context;

    public ItemCirclePicture(Context context) {
        this.context = context;
    }

    @Override
    public int getItemViewLayoutId() {
        return R.layout.news_item_newslist_circle_pic;
    }

    @Override
    public boolean isForViewType(GlobalNewsBean item, int position) {
        if ("15".equals(item.getType()) && item.getNewsStyle() == 2) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public void convert(ViewHolder holder, GlobalNewsBean bean, int position) {
        TextView title = holder.getView(R.id.title);
        title.setText(bean.getTitle());

        title.setTextColor(context.getResources().getColor(R.color.global_black_lv1));
        if (CommonUtils.getInstance().getHasHistory()) {
            if (Hawk.contains("readHistory")) {
                Map<String, String> historyMap = CommonUtils.getInstance().getReadHistory();
                for (Map.Entry<String, String> vo : historyMap.entrySet()) {
                    String newsId = (String) vo.getKey();
                    if (bean.getNewsId().equals(newsId)) {
                        title.setTextColor(context.getResources().getColor(R.color.global_gray_lv1));
                        break;
                    }
                }
            }
        }

        TextView topName = holder.getView(R.id.topic_name);
        if (StringUtils.isNotEmpty(bean.getOrigin())) {
            topName.setVisibility(View.VISIBLE);
            topName.setText("#" + bean.getOrigin() + "#");
        } else {
            topName.setVisibility(View.GONE);
        }

        TextView nickName = holder.getView(R.id.nickName);
        if (StringUtils.isNotEmpty(bean.getCatalogId())) {
            nickName.setText(bean.getCatalogId());
        } else {
            nickName.setText("匿名");
        }

        TextView scan = holder.getView(R.id.scan);
        try {
            int minPlay = Integer.parseInt(CommonUtils.getInstance().getMinPlay());
            if (minPlay == 0) {
                scan.setVisibility(View.GONE);
            } else {
                //判断评论数是否大于最少浏览数
                if (StringUtils.isEmpty(bean.getScanNum() + "")) {
                    scan.setVisibility(View.GONE);
                } else if (bean.getScanNum() >=
                        minPlay) {
                    scan.setText(StringUtils.transformNum(bean.getScanNum() + "") + "阅");
                    scan.setVisibility(View.VISIBLE);
                } else {
                    scan.setVisibility(View.GONE);
                }
            }

        } catch (Exception e) {
            scan.setVisibility(View.GONE);
        }

        LinearLayout image_layout = holder.getView(R.id.image_layout);

        RoundedImageView image_one = holder.getView(R.id.image_one);
        final CardView image_one_layout = holder.getView(R.id.image_one_layout);


        RoundedImageView image_two = holder.getView(R.id.image_two);
        final CardView image_two_layout = holder.getView(R.id.image_two_layout);

        RoundedImageView image_three = holder.getView(R.id.image_three);
        final CardView image_three_layout = holder.getView(R.id.image_three_layout);
        TextView photo_tag = holder.getView(R.id.photo_tag);


        switch (bean.getCommentNum()) {
            case 0:
                image_layout.setVisibility(View.GONE);
                break;
            case 1:
                image_layout.setVisibility(View.VISIBLE);
                image_one_layout.setVisibility(View.VISIBLE);
                image_two_layout.setVisibility(View.INVISIBLE);
                image_three_layout.setVisibility(View.INVISIBLE);
                photo_tag.setVisibility(View.GONE);
                Glide.with(context)
                        .load(bean.getPictureOne())
                        .apply(new RequestOptions().placeholder(R.drawable.cover_normal_default).error(R.drawable.cover_normal_default))
                        .into(image_one);
                break;
            case 2:
                image_layout.setVisibility(View.VISIBLE);
                image_one_layout.setVisibility(View.VISIBLE);
                image_two_layout.setVisibility(View.VISIBLE);
                image_three_layout.setVisibility(View.INVISIBLE);
                photo_tag.setVisibility(View.GONE);
                Glide.with(context)
                        .load(bean.getPictureOne())
                        .apply(new RequestOptions().placeholder(R.drawable.cover_normal_default).error(R.drawable.cover_normal_default))
                        .into(image_one);
                Glide.with(context)
                        .load(bean.getPictureTwo())
                        .apply(new RequestOptions().placeholder(R.drawable.cover_normal_default).error(R.drawable.cover_normal_default))
                        .into(image_two);
                break;
            case 3:
                image_layout.setVisibility(View.VISIBLE);
                image_one_layout.setVisibility(View.VISIBLE);
                image_two_layout.setVisibility(View.VISIBLE);
                image_three_layout.setVisibility(View.VISIBLE);
                photo_tag.setVisibility(View.GONE);
                Glide.with(context)
                        .load(bean.getPictureOne())
                        .apply(new RequestOptions().placeholder(R.drawable.cover_normal_default).error(R.drawable.cover_normal_default))
                        .into(image_one);
                Glide.with(context)
                        .load(bean.getPictureTwo())
                        .apply(new RequestOptions().placeholder(R.drawable.cover_normal_default).error(R.drawable.cover_normal_default))
                        .into(image_two);
                Glide.with(context)
                        .load(bean.getPictureThree())
                        .apply(new RequestOptions().placeholder(R.drawable.cover_normal_default).error(R.drawable.cover_normal_default))
                        .into(image_three);
                break;
            default:
                image_layout.setVisibility(View.VISIBLE);
                image_one_layout.setVisibility(View.VISIBLE);
                image_two_layout.setVisibility(View.VISIBLE);
                image_three_layout.setVisibility(View.VISIBLE);
                photo_tag.setVisibility(View.VISIBLE);
                Glide.with(context)
                        .load(bean.getPictureOne())
                        .apply(new RequestOptions().placeholder(R.drawable.cover_normal_default).error(R.drawable.cover_normal_default))
                        .into(image_one);
                Glide.with(context)
                        .load(bean.getPictureTwo())
                        .apply(new RequestOptions().placeholder(R.drawable.cover_normal_default).error(R.drawable.cover_normal_default))
                        .into(image_two);
                Glide.with(context)
                        .load(bean.getPictureThree())
                        .apply(new RequestOptions().placeholder(R.drawable.cover_normal_default).error(R.drawable.cover_normal_default))
                        .into(image_three);

                TextView photoNum = holder.getView(R.id.photo_tag);
                photoNum.setText("共" + bean.getCommentNum() + "张");
                break;
        }
    }
}
